<template>
<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="50px">
  <el-form-item label="账号" prop="account">
    <el-input v-model.number="ruleForm.account"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>

<script>
export default {
  data() {
    var checkAccont = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('输入错误'));
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        account: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        account: [
          { validator: checkAccont, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$emit('loginSubmit', this.ruleForm)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-form{
  width: 70%;
  min-height: 370px;
  margin: 0px auto;
  padding-top: 15%;
}
</style>
